package com.ruoyi.web.controller.business.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.goods.domain.JbPackageOfflinePrice;
import com.ruoyi.business.goods.service.JbPackageOfflinePriceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 套餐线下价格 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("套餐线下价格")
@RestController
@RequestMapping("/package_Offline_price")
public class JbPackageOfflinePriceController extends JoController
{
    @Autowired
    private JbPackageOfflinePriceService service;

    /**
     * 查询套餐线下价格列表
     */
    @ApiOperation("查询套餐线下价格列表")
    @PreAuthorize("@ss.hasPermi('package_Offline_price:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbPackageOfflinePrice jbPackageOfflinePrice)
    {
        LambdaQueryWrapper<JbPackageOfflinePrice> select = new LambdaQueryWrapper<>();
        IPage<JbPackageOfflinePrice> iPage = service.page(page.b(), select);
        return JoR.rows(iPage);
    }

    /**
     * 查询全部
     *
     * @param pid 上级id
     * @return
     */
    public List<JbPackageOfflinePrice> listAll(Long pid)
    {
        LambdaQueryWrapper<JbPackageOfflinePrice> select = new LambdaQueryWrapper<>();
        select.eq(JbPackageOfflinePrice::getPackageId, pid);
        return service.list(select);
    }

    /**
     * 导出套餐线下价格列表
     */
    @ApiOperation("导出套餐线下价格列表")
    @PreAuthorize("@ss.hasPermi('package_Offline_price:export')")
    @Log(title = "套餐线下价格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbPackageOfflinePrice jbPackageOfflinePrice)
    {
        LambdaQueryWrapper<JbPackageOfflinePrice> select = new LambdaQueryWrapper<>();
        IPage<JbPackageOfflinePrice> iPage = service.page(page.b(), select);

        List<JbPackageOfflinePrice> list = iPage.getRecords();
        ExcelUtil<JbPackageOfflinePrice> util = new ExcelUtil<JbPackageOfflinePrice>(JbPackageOfflinePrice.class);
        util.exportExcel(response, list, "套餐线下价格数据");
    }

    /**
     * 获取套餐线下价格详细信息
     */
    @ApiOperation("获取套餐线下价格详细信息")
    @PreAuthorize("@ss.hasPermi('package_Offline_price:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增套餐线下价格
     */
    @ApiOperation("新增套餐线下价格")
    @PreAuthorize("@ss.hasPermi('package_Offline_price:add')")
    @Log(title = "套餐线下价格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbPackageOfflinePrice jbPackageOfflinePrice)
    {
        return toAjax(service.save(jbPackageOfflinePrice));
    }

    /**
     * 批量新增套餐线下价格
     */
    @Log(title = "批量新增套餐线下价格", businessType = BusinessType.INSERT)
    public boolean adds(List<JbPackageOfflinePrice> list, Long pid)
    {
        for (JbPackageOfflinePrice price : list)
        {
            price.setPackageId(pid);
        }

        return service.saveBatch(list);
    }

    /**
     * 修改套餐商品
     */
    public boolean edits(List<JbPackageOfflinePrice> list, Long id)
    {
        for (JbPackageOfflinePrice packageOfflinePrice : list)
        {
            packageOfflinePrice.setPackageId(id);
        }
        return service.saveOrUpdateBatch(list);
    }

    /**
     * 修改套餐线下价格
     */
    @ApiOperation("修改套餐线下价格")
    @PreAuthorize("@ss.hasPermi('package_Offline_price:edit')")
    @Log(title = "套餐线下价格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbPackageOfflinePrice jbPackageOfflinePrice)
    {
        return toAjax(service.updateById(jbPackageOfflinePrice));
    }

    /**
     * 删除套餐线下价格
     */
    @ApiOperation("删除套餐线下价格")
    @PreAuthorize("@ss.hasPermi('package_Offline_price:remove')")
    @Log(title = "套餐线下价格", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }
}
